package com.jnmd.video.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jnmd.video.pojo.Course;
import com.jnmd.video.pojo.CourseQueryVo;
import com.jnmd.video.pojo.Speaker;
import com.jnmd.video.pojo.Subject;
import com.jnmd.video.pojo.Video;
import com.jnmd.video.pojo.VideoQueryVo;
import com.jnmd.video.service.CourseService;
import com.jnmd.video.service.SpeakerService;
import com.jnmd.video.service.SubjectService;
import com.jnmd.video.service.VideoService;
import com.jnmd.video.utils.Page;
import com.jnmd.video.utils.TimeUtils;

@Controller
@RequestMapping("video")
public class VideoController {
	
	@Autowired
	private VideoService videoService;
	
	@Autowired
	private SpeakerService speakerService;
	
	@Autowired
	private CourseService courseService;
	
	@Autowired
	private SubjectService subjectService;
	
	@RequestMapping("/showVideo")
	public ModelAndView showVideoList(VideoQueryVo queryVo){
		
		System.out.println("进入showVideoList方法");
		
		
		ModelAndView modelAndView =new ModelAndView();
		queryVo.setBegin((queryVo.getPage()-1)*queryVo.getRows());
		List<Video> list =videoService.findVideoByVo(queryVo);
		for (Video video2 : list) {
			if(null!=video2.getTime() && video2.getTime()!=0){
				video2.setShowTime(TimeUtils.getHMS(video2.getTime()));
			}
			
			
		}
		modelAndView.addObject("list",list );
		modelAndView.addObject("speakerList", speakerService.findSpeakerAll());
		
		modelAndView.addObject("courseList", courseService.findCourseAll());
		Page<Video> page =new Page<Video>();
		page.setPage(queryVo.getPage());
		page.setSize(queryVo.getRows());
		page.setTotal(videoService.getCountVideo(queryVo));
//		page.setRows(list);
		modelAndView.addObject("page",page);
		
		modelAndView.setViewName("admin/video/index");
		return modelAndView;
	}
	
	
	
	@RequestMapping("/editVideo")
	public String editVideo(int id,Model model){
		
		//修改
		if(id!=-1){
			
			Video video = videoService.findVideoById(id);
			
			model.addAttribute("video", video);
			
		}
		//新增
		
		model.addAttribute("speakerList", speakerService.findSpeakerAll());
		model.addAttribute("courseList", courseService.findCourseAll());
		
		return "admin/video/edit";
		
	}
	
	@RequestMapping("batchDelete.action")
	public String batchDelete(Integer[] ids){
		
		
		//批量删除
		videoService.batchDeleteByIds(ids);
		
		return "redirect:showVideo.action";
	}
	
	@RequestMapping("saveOrUpdateVideo.action")
	public String saveOrUpdateVideo(Video video){
		
		//修改
		if(video.getId()!=null && video.getId()!=0){
			videoService.updateVideo(video);
		//新增
		}else{
			video.setPlayNum(0);
			videoService.saveVideo(video);
		}
		
		return "redirect:showVideo.action";
	}
	
	@RequestMapping("deleteVideo.action")
	@ResponseBody //处理结果，不走视图解析器，而且会将数据转换成为json,但是如果是String，不用转
	public String deleteVideo(int id){
		
		videoService.deleteVideoById(id);
		return "success";
	}
	
	
	@RequestMapping("/showItem")
	public ModelAndView showVideoList1(CourseQueryVo course){
		
		
		ModelAndView modelAndView =new ModelAndView();
		System.out.println("&&&&&&&&&&&&&&&&&&&&&"+course);
		course.setBegin((course.getPage()-1)*course.getRows());
		List<Course> list =courseService.findCourseSome(course);
		
		modelAndView.addObject("list",list );
		Page<Course> page =new Page<Course>();
		page.setPage(course.getPage());
		page.setSize(course.getRows());
		page.setTotal(courseService.findCourseAll().size());
//		page.setRows(list);
		modelAndView.addObject("page",page);
		modelAndView.setViewName("admin/video/item");
		return modelAndView;
	}
	
	@RequestMapping("/editItem")
	public ModelAndView showVideoList1(int id){
		System.out.println("***********"+id+"************");
		ModelAndView modelAndView =new ModelAndView();
		Course course = courseService.selectCourseById(id);
		List<Subject> subjectList = subjectService.selectAllSubject();
		modelAndView.addObject("course",course);
		modelAndView.addObject("subjectList",subjectList);
		modelAndView.setViewName("admin/video/addItem");
		return modelAndView;
	}
	@RequestMapping("/saveItem")
	public String showVideoList2(Course course){
		
		if(course.getId()!=null && course.getId()!=0){
			courseService.updateCourse(course);
		//新增
		}else{
			courseService.saveCourse(course);
		    }
		
		return "redirect:showItem.action";
	}
	
	@RequestMapping("/delItem")
	@ResponseBody
	public String showVideoList3(int id){
		courseService.deleteCourseById(id);
		return "success";
	}
	
	
/*                      主讲人管理                                                              */
	@RequestMapping("/showTecher")
	public ModelAndView showTecherList2(CourseQueryVo course){
		
		
		ModelAndView modelAndView =new ModelAndView();
	
		course.setBegin((course.getPage()-1)*course.getRows());
		List<Speaker> list = speakerService.findSpeakerSome(course);
		modelAndView.addObject("list",list );
		Page<Speaker> page =new Page<Speaker>();
		page.setPage(course.getPage());
		page.setSize(course.getRows());
		page.setTotal(speakerService.findSpeakerAll().size());
//		page.setRows(list);
		modelAndView.addObject("page",page);
		modelAndView.setViewName("admin/video/techer");
		return modelAndView;
	}
	
	@RequestMapping("/editTecher")
	public ModelAndView showTecherList2(int id){
		System.out.println("***********"+id+"************");
		ModelAndView modelAndView =new ModelAndView();
		Speaker course = speakerService.selectSpeakerById(id);
		modelAndView.addObject("speaker",course);
		modelAndView.setViewName("admin/video/addTecher");
		return modelAndView;
	}
	@RequestMapping("/saveTecher")
	public String showVideoList4(Speaker speaker){
		System.out.println("SSSSSSSSSSSSSSSSSS");
		
		speakerService.saveSpeaker(speaker);
		return "redirect:showTecher.action";
	}
	
	@RequestMapping("/delTecher")
	@ResponseBody
	public String showTecherList4(int id){
		speakerService.deleteSpeakerById(id);
		
		return "success";
	}

	
}
